/*
* Copyright (C) 2019 ~ 2021 Uniontech Software Technology Co.,Ltd.
*
* Author:     yanghongwei <yanghongwei@uniontech.com>
*
* Maintainer: yanghongwei <yanghongwei@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"

#include <QFile>
#include <QDebug>

#define U_DISK_NOTIFY_LOCAL_FILE    "/udisk_notify_history"
bool saveLocal(const ReportNode &node)
{
    QFile file(U_DISK_NOTIFY_LOCAL_FILE);
    if (!file.open(QIODevice::Append | QIODevice::Text)) {
        qWarning() << "udisk notify history file open error.";
        return false; 
    }

    QTextStream out(&file);
    out << node.m_fileName << "=" << node.m_date << "=" << node.m_uDiskDir << "=" << node.m_operation << "\n";

    file.close();
    return true;
}

QList<ReportNode> readLock()
{
    QFile file(U_DISK_NOTIFY_LOCAL_FILE);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "udisk notify history file open error.";
        return QList<ReportNode>(); 
    }

    QList<ReportNode> listNode;
    ReportNode node;
    do
    {
        QString stringNode(file.readLine());
        if (!stringNode.isEmpty()) {
            QStringList infoList = stringNode.split("=");
            node.m_fileName = infoList[0];
            node.m_date = infoList[1];
            node.m_uDiskDir = infoList[2];
            node.m_operation = infoList[3];
            listNode.append(node);
        }
    } while (!file.atEnd());

    file.close();
    return listNode;
}